/**
 * 合并两个类的实例属性
 * @file Mixin.js
 * @function Mixin
 * @author iwangcj
 */

function copyProperties(target, source) {
    for (let key of Reflect.ownKeys(source)) {
      if ( key !== 'constructor'
        && key !== 'prototype'
        && key !== 'name'
      ) {
          if (!Reflect.has(target, key)) {
            let desc = Reflect.getOwnPropertyDescriptor(source, key);
            Reflect.defineProperty(target, key, desc);
          }
      }
    }
}

/**
 * 合并两个类的实例属性
 * @param {*} originalClass 原始类
 * @param {*} extendClass 扩展类
 */
const Mixin = function(originalClass, extendClass) {
    if (originalClass && extendClass) {
        originalClass = originalClass || class Mix{}
        copyProperties(originalClass, extendClass)
        copyProperties(originalClass.prototype, extendClass.prototype);

        const extendClassObj = new extendClass();
        Object.getOwnPropertyNames(extendClassObj).forEach(function(key) {
            if (!Reflect.has(originalClass, key)) {
                let desc = Reflect.getOwnPropertyDescriptor(extendClassObj, key);
                Reflect.defineProperty(originalClass.prototype, key, desc);
            }
        });
    }

    return originalClass;
}


export default Mixin;